package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.GoodsReturnedDto;
import com.qf.commerce.entity.GoodsReturned;
import com.qf.commerce.exception.ServiceException;
import com.qf.commerce.mapper.GoodsReturnedMapper;
import com.qf.commerce.resp.ResponseStatus;
import com.qf.commerce.service.GoodsReturnedService;
import com.qf.commerce.utils.CommonBeanUtils;
import com.qf.commerce.common.vo.GoodsReturnedVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class GoodsReturnedServiceImpl implements GoodsReturnedService {
    @Resource
    GoodsReturnedMapper goodsReturnedMapper;

    @Override
    public PageInfo<GoodsReturnedVo> queryGoodsReturned(int page,int size,GoodsReturnedDto goodsReturnedDto) {
        PageInfo<GoodsReturnedVo> goodsReturnedVoPageInfo = CommonBeanUtils.copyPage(PageHelper.startPage(page, size).doSelectPageInfo(() -> goodsReturnedMapper.selectGoodsReturnedMapper(goodsReturnedDto)), new PageInfo<GoodsReturnedVo>(), GoodsReturnedVo::new);
        if(ObjectUtils.isEmpty(goodsReturnedVoPageInfo)){
            throw new ServiceException(ResponseStatus.PAGE_ERROR);
        }
        return goodsReturnedVoPageInfo;
    }

    @Override
    public int delGoodReturnedById(List<Long> returnOrderId) {
        int num = goodsReturnedMapper.deleteById(returnOrderId);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public int upDeliveryById(List<Long> returnOrderId) {
        //先查询批量收货字段是否正确
        List<GoodsReturned> list = goodsReturnedMapper.selectDeliverById(returnOrderId);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        int num = goodsReturnedMapper.updateDeliverById(returnOrderId);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public int upReturnMoneyById(List<Long> returnOrderId, BigDecimal returnMoney) {
        //先查询批量退款数据是否正确
        List<GoodsReturned> list = goodsReturnedMapper.selectReturnMoneyById(returnOrderId);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        for (GoodsReturned goodsReturned : list) {
            int num = returnMoney.compareTo(goodsReturned.getOrderList().getGrossMerchandise());
            if(num>0){
                throw new ServiceException(ResponseStatus.DATA_ERROR);
            }
        }
        int num = goodsReturnedMapper.updateReturnMoneyById(returnOrderId,returnMoney);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public List<GoodsReturnedVo> queryGoodsZone() {
        List<GoodsReturned> list = goodsReturnedMapper.selectGoodsZone();
        if(ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return CommonBeanUtils.copyForList(list, GoodsReturnedVo::new);
    }

    @Override
    public List<GoodsReturnedVo> queryGoodsOne() {
        List<GoodsReturned> list = goodsReturnedMapper.selectGoodsOne();
        if(ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return CommonBeanUtils.copyForList(list, GoodsReturnedVo::new);
    }

    @Override
    public List<GoodsReturnedVo> queryGoodsTwo() {
        List<GoodsReturned> list = goodsReturnedMapper.selectGoodsTwo();
        if(ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return CommonBeanUtils.copyForList(list, GoodsReturnedVo::new);
    }

    @Override
    public List<GoodsReturnedVo> queryGoodsThree() {
        List<GoodsReturned> list = goodsReturnedMapper.selectGoodsThree();
        if(ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return CommonBeanUtils.copyForList(list, GoodsReturnedVo::new);
    }

    @Override
    public List<GoodsReturnedVo> queryGoodsFour() {
        List<GoodsReturned> list = goodsReturnedMapper.selectGoodsFour();
        if(ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return CommonBeanUtils.copyForList(list, GoodsReturnedVo::new);
    }

    @Override
    public List<GoodsReturnedVo> queryGoodsFive() {
        List<GoodsReturned> list = goodsReturnedMapper.selectGoodsFive();
        if(ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return CommonBeanUtils.copyForList(list, GoodsReturnedVo::new);
    }

    @Override
    public List<GoodsReturnedVo> queryGoodsLower() {
        List<GoodsReturned> list = goodsReturnedMapper.selectGoodsLower();
        if(ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return CommonBeanUtils.copyForList(list, GoodsReturnedVo::new);
    }

    @Override
    public GoodsReturnedVo queryGoodsDetail(Long returnOrderId) {
        GoodsReturned goodsReturned = goodsReturnedMapper.selectGoodsReturnedById(returnOrderId);
        if(ObjectUtils.isEmpty(goodsReturned)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        if(goodsReturned.getReturnStatus()==-1){
            goodsReturned.setReturnMoney(null);
            goodsReturned.getOrderList().getCommodity1().setCommodityPrice(null);
            goodsReturned.setReturnTrackingNumber(null);
            goodsReturned.getWarehouses().forEach(s->{s.setAddress(null);s.setAddressDetail(null);s.setLinkman(null);s.setPhone(null);s.setPostalCode(null);});
        }
        return CommonBeanUtils.copyForBean(goodsReturned,GoodsReturnedVo::new);
    }

    @Override
    public int upGoodsReturnInject(GoodsReturnedDto goodsReturnedDto) {
        GoodsReturned goodsReturned = CommonBeanUtils.copyForBean(goodsReturnedDto, GoodsReturned::new);
        int num = goodsReturnedMapper.updateReturnInject(goodsReturned);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public int upGoodsReturnCan(GoodsReturnedDto goodsReturnedDto) {
        GoodsReturned goodsReturned = CommonBeanUtils.copyForBean(goodsReturnedDto, GoodsReturned::new);
        int num = goodsReturnedMapper.updateReturnCan(goodsReturned);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public int upReturnReceive(GoodsReturnedDto goodsReturnedDto) {
        GoodsReturned goodsReturned = CommonBeanUtils.copyForBean(goodsReturnedDto, GoodsReturned::new);
        int num = goodsReturnedMapper.updateReturnReceive(goodsReturned);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public int upReturnAbnormalReceive(GoodsReturnedDto goodsReturnedDto) {
        GoodsReturned goodsReturned = CommonBeanUtils.copyForBean(goodsReturnedDto, GoodsReturned::new);
        int num = goodsReturnedMapper.updateReturnAbnormalReceive(goodsReturned);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        return num;
    }

    @Override
    public int upReturnMoney(GoodsReturnedDto goodsReturnedDto) {
        GoodsReturned goodsReturned = CommonBeanUtils.copyForBean(goodsReturnedDto, GoodsReturned::new);
        GoodsReturned goodsReturned1 =goodsReturnedMapper.selectGoodsMoney(goodsReturned);
        int num = goodsReturned.getOrderList().getGrossMerchandise().compareTo(goodsReturned1.getOrderList().getGrossMerchandise());
        if(num>0){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        //填写应退金额
        num = goodsReturnedMapper.updateReturnMoney(goodsReturned);
        return num;
    }

    @Override
    public int upAbnormalReturnReason(GoodsReturnedDto goodsReturnedDto) {
        GoodsReturned goodsReturned = CommonBeanUtils.copyForBean(goodsReturnedDto, GoodsReturned::new);
        int num = goodsReturnedMapper.updateReturnAbnormalReason(goodsReturned);
        if(num==0){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return num;
    }

    @Override
    public GoodsReturnedVo queryReturnWaybill(GoodsReturnedDto goodsReturnedDto) {
        //根据物流号查询物流信息
        GoodsReturned goodsReturned = CommonBeanUtils.copyForBean(goodsReturnedDto, GoodsReturned::new);
        goodsReturned = goodsReturnedMapper.selectReturnWaybill(goodsReturned);
        if(ObjectUtils.isEmpty(goodsReturned)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        GoodsReturnedVo goodsReturnedVo = CommonBeanUtils.copyForBean(goodsReturned, GoodsReturnedVo::new);
        return goodsReturnedVo;
    }
}
